package face.pay.web.oem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.dao.OemSmsDao;
import face.pay.entity.SmsConfig;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import face.pay.web.oem.service.IOemSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OemSmsService implements IOemSmsService {


    @Autowired
    private OemSmsDao oemSmsDao;

    /**
     * 查询短信配置信息
     * @param smsConfig
     * @param pageParam
     * @return
     */
    @Override
    public PageData queryPage(SmsConfig smsConfig, PageParam pageParam) {

        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());


        List<SmsConfig> list= this.oemSmsDao.queryList(smsConfig);
        //截取创建时间字符
        List<SmsConfig> sublist = list.stream().map(sms -> {
            if(sms.getCreatedTime()!=null){
                String s = sms.getCreatedTime().substring(0, 19);
                sms.setCreatedTime(s);
                return sms;
            }else {
                return sms;
            }

        }).collect(Collectors.toList());
        PageInfo<SmsConfig> pageInfo = new PageInfo <SmsConfig>(list);
        PageData<SmsConfig> pageData = new PageData<SmsConfig>(pageInfo.getTotal(),sublist);
        return pageData;
    }


    /**
     * 编辑短信配置信息
     * @param smsConfig
     * @return
     */
    @Override
    public ResultMessage editSmsInfo(SmsConfig smsConfig) {
        try {
            if(null==smsConfig.getSmsId()){
                //新增
                smsConfig.setSys_config_id(smsConfig.getSysId());
                smsConfig.setCreatedBy(smsConfig.getUpdatedBy());
                this.oemSmsDao.insert(smsConfig);
                return ResultMessage.createSuccess();
            }else {
                //修改
                this.oemSmsDao.update(smsConfig);
                return ResultMessage.createSuccess();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMessage.createFail();
        }
    }
}
